<template>
  <div>

    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-row justify="end" type="flex">
      <el-button type="primary" size="mini" @click="$emit('close')">取消</el-button>
      <el-button type="primary" size="mini" @click="hSubmit">确定</el-button>
    </el-row>

  </div>
</template>
<script>
import { getPermissionList } from '@/api/permissons'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 保存更新
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({ id: this.curId, permIds: permIds })
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.$emit('success')
    },
    // 多选框角色-数据回显--角色权限
    async  loadPermissionList() {
      try {
        const res = await getPermissionList()
        const detail = await getRoleDetail(this.curId)
        // detail.data
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        this.list = tranListToTreeData(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>
